﻿using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Logging;
using System;
using Loong.Threading.Locks;

namespace Loong.Runtime.Caching.Memory
{
    public class LoongMemoryCache : CacheBase
    {
        private readonly IMemoryCache _memoryCache;

        public LoongMemoryCache(
            ILockFactory lockFactory,
            ILogger<LoongMemoryCache> logger,
            IMemoryCache memoryCache)
            : base(lockFactory, logger)
        {
            _memoryCache = memoryCache;
        }

        public override object Get(string key)
        {
            return _memoryCache.Get(key);
        }

        public override void Set(string key, object value, TimeSpan expireTime)
        {
            if (value == null)
            {
                throw new LoongException("Can not insert null values to the cache!");
            }

            _memoryCache.Set(key, value, expireTime);
        }

        public override void Remove(string key)
        {
            _memoryCache.Remove(key);
        }

        public override void Clear()
        {
        }
    }
}
